﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

<DefaultProperty("ImageUrl"), _
ToolboxData("<{0}:Thumb runat=server></{0}:Thumb>"), _
ToolboxBitmap(GetType(Thumb), "IconThumb.png")> _
Public Class Thumb
    Inherits WebControl

#Region "Constants"

    Const _defaultImageWidth As Integer = 90
    Const _defaultImageHeight As Integer = 90
    Const _defaultImageQuality As Integer = 90

#End Region

#Region "Properties"

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(True), UrlProperty(), _
    Editor(GetType(System.Web.UI.Design.ImageUrlEditor), GetType(System.Drawing.Design.UITypeEditor))> _
    Property ImageUrl() As String
        Get
            Dim s As String = CStr(ViewState("ImageUrl"))
            If s Is Nothing Then
                Return String.Empty
            Else
                Return s.Trim
            End If
        End Get
        Set(ByVal Value As String)
            ViewState("ImageUrl") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(True)> _
    Property AlternateText() As String
        Get
            Dim s As String = CStr(ViewState("AlternateText"))
            If s Is Nothing Then
                Return String.Empty
            Else
                Return s.Trim
            End If
        End Get
        Set(ByVal Value As String)
            ViewState("AlternateText") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(False)> _
    Property ImageWidth() As Integer
        Get
            If IsNothing(ViewState("ImageWidth")) Then
                Return 0
            Else
                Return CInt(ViewState("ImageWidth"))
            End If
        End Get
        Set(ByVal Value As Integer)
            ViewState("ImageWidth") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(False)> _
    Property ImageHeight() As Integer
        Get
            If IsNothing(ViewState("ImageHeight")) Then
                Return 0
            Else
                Return CInt(ViewState("ImageHeight"))
            End If
        End Get
        Set(ByVal Value As Integer)
            ViewState("ImageHeight") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(False)> _
    Property ImageQuality() As Integer
        Get
            If IsNothing(ViewState("ImageQuality")) Then
                Return _defaultImageQuality
            Else
                Return CInt(ViewState("ImageQuality"))
            End If
        End Get
        Set(ByVal Value As Integer)
            ViewState("ImageQuality") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(True), _
    Editor(GetType(System.Web.UI.Design.ColorBuilder), GetType(System.Drawing.Design.UITypeEditor))> _
    Property ImageBackgroundColor() As Color
        Get
            If IsNothing(ViewState("ImageBackgroundColor")) Then
                Return Color.White
            Else
                Return CType(ViewState("ImageBackgroundColor"), Color)
            End If
        End Get
        Set(ByVal Value As Color)
            ViewState("ImageBackgroundColor") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(False)> _
    Property CornerType() As Options.CornerTypes
        Get
            If IsNothing(ViewState("CornerType")) Then
                Return Options.CornerTypes.None
            Else
                Return CType(ViewState("CornerType"), Options.CornerTypes)
            End If
        End Get
        Set(ByVal Value As Options.CornerTypes)
            ViewState("CornerType") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(False)> _
    Property TopLeftCornerType() As Options.CornerTypes
        Get
            If IsNothing(ViewState("TopLeftCornerType")) Then
                Return Options.CornerTypes.None
            Else
                Return CType(ViewState("TopLeftCornerType"), Options.CornerTypes)
            End If
        End Get
        Set(ByVal Value As Options.CornerTypes)
            ViewState("TopLeftCornerType") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(False)> _
    Property TopRightCornerType() As Options.CornerTypes
        Get
            If IsNothing(ViewState("TopRightCornerType")) Then
                Return Options.CornerTypes.None
            Else
                Return CType(ViewState("TopRightCornerType"), Options.CornerTypes)
            End If
        End Get
        Set(ByVal Value As Options.CornerTypes)
            ViewState("TopRightCornerType") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(False)> _
    Property BottomRightCornerType() As Options.CornerTypes
        Get
            If IsNothing(ViewState("BottomRightCornerType")) Then
                Return Options.CornerTypes.None
            Else
                Return CType(ViewState("BottomRightCornerType"), Options.CornerTypes)
            End If
        End Get
        Set(ByVal Value As Options.CornerTypes)
            ViewState("BottomRightCornerType") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(False)> _
    Property BottomLeftCornerType() As Options.CornerTypes
        Get
            If IsNothing(ViewState("BottomLeftCornerType")) Then
                Return Options.CornerTypes.None
            Else
                Return CType(ViewState("BottomLeftCornerType"), Options.CornerTypes)
            End If
        End Get
        Set(ByVal Value As Options.CornerTypes)
            ViewState("BottomLeftCornerType") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(False)> _
    Property CornerRadius() As Integer
        Get
            If IsNothing(ViewState("CornerRadius")) Then
                Return 0
            Else
                Return CInt(ViewState("CornerRadius"))
            End If
        End Get
        Set(ByVal Value As Integer)
            ViewState("CornerRadius") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(False)> _
    Property TopLeftCornerRadius() As Integer
        Get
            If IsNothing(ViewState("TopLeftCornerRadius")) Then
                Return 0
            Else
                Return CInt(ViewState("TopLeftCornerRadius"))
            End If
        End Get
        Set(ByVal Value As Integer)
            ViewState("TopLeftCornerRadius") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(False)> _
    Property TopRightCornerRadius() As Integer
        Get
            If IsNothing(ViewState("TopRightCornerRadius")) Then
                Return 0
            Else
                Return CInt(ViewState("TopRightCornerRadius"))
            End If
        End Get
        Set(ByVal Value As Integer)
            ViewState("TopRightCornerRadius") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(False)> _
    Property BottomLeftCornerRadius() As Integer
        Get
            If IsNothing(ViewState("BottomLeftCornerRadius")) Then
                Return 0
            Else
                Return CInt(ViewState("BottomLeftCornerRadius"))
            End If
        End Get
        Set(ByVal Value As Integer)
            ViewState("BottomLeftCornerRadius") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(False)> _
    Property BottomRightCornerRadius() As Integer
        Get
            If IsNothing(ViewState("BottomRightCornerRadius")) Then
                Return 0
            Else
                Return ViewState("BottomRightCornerRadius")
            End If
        End Get
        Set(ByVal Value As Integer)
            ViewState("BottomRightCornerRadius") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(False)> _
    Property ImageBorder() As DashStyle
        Get
            If IsNothing(ViewState("ImageBorder")) Then
                Return -1
            Else
                Return CType(ViewState("ImageBorder"), DashStyle)
            End If
        End Get
        Set(ByVal Value As DashStyle)
            ViewState("ImageBorder") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(False)> _
    Property ImageBorderWidth() As Integer
        Get
            If IsNothing(ViewState("ImageBorderWidth")) Then
                Return 1
            Else
                Return CInt(ViewState("ImageBorderWidth"))
            End If
        End Get
        Set(ByVal Value As Integer)
            ViewState("ImageBorderWidth") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(True), _
    Editor(GetType(System.Web.UI.Design.ColorBuilder), GetType(System.Drawing.Design.UITypeEditor))> _
    Property ImageBorderColor() As Color
        Get
            If IsNothing(ViewState("ImageBorderColor")) Then
                Return Color.Black
            Else
                Return CType(ViewState("ImageBorderColor"), Color)
            End If
        End Get
        Set(ByVal Value As Color)
            ViewState("ImageBorderColor") = Value
        End Set
    End Property

    <Bindable(True), Category("Image"), DefaultValue(""), Localizable(False)> _
    Property ImageFilter() As Options.ImageFilters
        Get
            If IsNothing(ViewState("ImageFilter")) Then
                Return Options.ImageFilters.None
            Else
                Return CType(ViewState("ImageFilter"), Options.ImageFilters)
            End If
        End Get
        Set(ByVal Value As Options.ImageFilters)
            ViewState("ImageFilter") = Value
        End Set
    End Property

    <Bindable(True), Category("Copyright"), DefaultValue(""), Localizable(True)> _
    Property CopyrightText() As String
        Get
            Dim s As String = CStr(ViewState("CopyrightText"))
            If s Is Nothing Then
                Return String.Empty
            Else
                Return s.Trim
            End If
        End Get
        Set(ByVal Value As String)
            ViewState("CopyrightText") = Value
        End Set
    End Property

    <Bindable(True), Category("Watermark"), DefaultValue(""), Localizable(True), UrlProperty(), _
    Editor(GetType(System.Web.UI.Design.ImageUrlEditor), GetType(System.Drawing.Design.UITypeEditor))> _
    Property WatermarkImageUrl() As String
        Get
            Dim s As String = CStr(ViewState("WatermarkImageUrl"))
            If s Is Nothing Then
                Return String.Empty
            Else
                Return s.Trim
            End If
        End Get
        Set(ByVal Value As String)
            ViewState("WatermarkImageUrl") = Value
        End Set
    End Property

    <Bindable(True), Category("Hyperlink"), DefaultValue(""), Localizable(True), UrlProperty(), _
    Editor(GetType(System.Web.UI.Design.UrlEditor), GetType(System.Drawing.Design.UITypeEditor))> _
    Property HyperlinkUrl() As String
        Get
            Dim s As String = CStr(ViewState("HyperlinkUrl"))
            If s Is Nothing Then
                Return String.Empty
            Else
                Return s.Trim
            End If
        End Get
        Set(ByVal Value As String)
            ViewState("HyperlinkUrl") = Value
        End Set
    End Property

    <Bindable(True), Category("Hyperlink"), DefaultValue(""), Localizable(False), _
    TypeConverterAttribute(GetType(TargetConverter))> _
    Property HyperlinkTarget() As String
        Get
            Dim s As String = CStr(ViewState("HyperlinkTarget"))
            If s Is Nothing Then
                Return String.Empty
            Else
                Return s.Trim
            End If
        End Get
        Set(ByVal Value As String)
            ViewState("HyperlinkTarget") = Value
        End Set
    End Property

#End Region

#Region "Subs"

    Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
        'Convert relative path to virtual.
        If Me.ImageUrl.StartsWith("../") Then
            Me.ImageUrl = Helpers.GetVirtualFromRelativePath(Me.ImageUrl)
        End If

        'Image path with querystring.
        Dim handlerPath As String = Helpers.GetHandlerPath(GetType(ThumbHandler))
        Dim imagePath As String

        'Add default image size.
        If Me.ImageWidth = 0 And Me.ImageHeight = 0 Then
            Me.ImageWidth = _defaultImageWidth
            Me.ImageHeight = _defaultImageHeight
        End If

        'Add required properties of image.
        imagePath = String.Format("{0}?a={1}&b={2}&c={3}&d={4}&e={5}", _
                                  Me.ResolveClientUrl(handlerPath), _
                                  Crypto.Encrypt(Me.ImageUrl), _
                                  Me.ImageWidth, _
                                  Me.ImageHeight, _
                                  Me.ImageQuality, _
                                  HttpContext.Current.Server.UrlEncode(Helpers.GetHexColor(Me.ImageBackgroundColor)))

        'Optional properties.
        'Corner radius.
        If Me.CornerType <> Options.CornerTypes.None Then
            'Add corner radius.
            If Me.CornerRadius <> Nothing Then
                imagePath &= String.Format("&f={0}&g={1}", CInt(Me.CornerType), Me.CornerRadius)
            End If
            'Add specified corner type and radius is different than general corner radius.
            If Me.TopLeftCornerType <> Options.CornerTypes.None And Me.TopLeftCornerType <> Me.CornerType Then imagePath &= String.Format("&h={0}", CInt(Me.TopLeftCornerType))
            If Me.TopRightCornerType <> Options.CornerTypes.None And Me.TopRightCornerType <> Me.CornerType Then imagePath &= String.Format("&i={0}", CInt(Me.TopRightCornerType))
            If Me.BottomRightCornerType <> Options.CornerTypes.None And Me.BottomRightCornerType <> Me.CornerType Then imagePath &= String.Format("&j={0}", CInt(Me.BottomRightCornerType))
            If Me.BottomLeftCornerType <> Options.CornerTypes.None And Me.BottomLeftCornerType <> Me.CornerType Then imagePath &= String.Format("&k={0}", CInt(Me.BottomLeftCornerType))
            If Me.TopLeftCornerRadius > 0 And Me.TopLeftCornerRadius <> Me.CornerRadius Then imagePath &= String.Format("&l={0}", Me.TopLeftCornerRadius)
            If Me.TopRightCornerRadius > 0 And Me.TopRightCornerRadius <> Me.CornerRadius Then imagePath &= String.Format("&m={0}", Me.TopRightCornerRadius)
            If Me.BottomRightCornerRadius > 0 And Me.BottomRightCornerRadius <> Me.CornerRadius Then imagePath &= String.Format("&n={0}", Me.BottomRightCornerRadius)
            If Me.BottomLeftCornerRadius > 0 And Me.BottomLeftCornerRadius <> Me.CornerRadius Then imagePath &= String.Format("&o={0}", Me.BottomLeftCornerRadius)
        End If

        'Border.
        If Me.ImageBorder > -1 Then
            imagePath &= String.Format("&p={0}&q={1}&r={2}", CInt(Me.ImageBorder), CInt(Me.ImageBorderWidth), HttpContext.Current.Server.UrlEncode(Helpers.GetHexColor(Me.ImageBorderColor)))
        End If

        'Filter.
        If Me.ImageFilter <> Options.ImageFilters.None Then
            imagePath &= String.Format("&s={0}", CInt(Me.ImageFilter))
        End If

        'Watermark.
        If Me.WatermarkImageUrl.Trim.Length > 0 Then
            If Me.WatermarkImageUrl.StartsWith("../") Then Me.WatermarkImageUrl = Helpers.GetVirtualFromRelativePath(Me.WatermarkImageUrl)
            imagePath &= String.Format("&t={0}", Crypto.Encrypt(Me.WatermarkImageUrl))
        End If

        'Copyright.
        If Me.CopyrightText.Trim.Length > 0 Then
            imagePath &= String.Format("&u={0}", HttpContext.Current.Server.UrlEncode(Me.CopyrightText))
        End If

        'Write hyperlink tag.
        If Me.HyperlinkUrl.Trim.Length > 0 Then
            If Me.HyperlinkUrl.StartsWith("~/") Then
                Me.HyperlinkUrl = Me.ResolveClientUrl(Me.HyperlinkUrl)
            End If
            writer.Write(String.Format("<a href=""{0}""{1}>", Me.HyperlinkUrl, IIf(Me.HyperlinkTarget.Length > 0, String.Format(" target=""{0}""", Me.HyperlinkTarget), String.Empty)))
        End If

        'Write image tag.
        writer.Write(String.Format("<img src=""{0}"" alt=""{1}""/>", imagePath, Me.AlternateText))

        'Close hyperlink tag.
        If Me.HyperlinkUrl.Trim.Length > 0 Then
            writer.Write("</a>")
        End If
    End Sub

#End Region

End Class
